//
//  UITableViewController+HDProvinceCity.m
//  app
//
//  Created by D on 15/5/11.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDProvinceCityController.h"
#import "HDPrvinceCityHttpResult.h"
#import "HDPrvinceCityDataManager.h"
#import "HDProvinceModel.h"
#import "HDCityAreaController.h"
#import "HDCityButton.h"


static const CGFloat kCartTotalPanelHeight = 120;

@interface HDProvinceCityController ()

@property (nonatomic,strong) HDPrvinceCityHttpResult * provinceCityResult;
@property (nonatomic,strong) HDPrvinceCityDataManager * provinceCityManager;
@property (nonatomic,strong) NSMutableArray * headerViewArray;
@property (nonatomic,strong) NSArray * headerTitleArr;
@property (nonatomic,strong) NSMutableArray * isOpens;
@property (nonatomic,assign) CGFloat cellHight;
@end


@implementation HDProvinceCityController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择城市";
    [self.navigationController setNavigationBarHidden:NO];
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 0),
                                                                         [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight oh:-(self.navigationAndStatusBarHeight+ktabBarHeight)]) style:UITableViewStyleGrouped];
    self.tableView = tableView;
   
    
    self.tableView.sectionFooterHeight = 0;
    self.headerViewArray = [NSMutableArray array];
    self.headerTitleArr = @[@"热门城市",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"P",@"Q",@"R",@"S",@"T",@"W",@"X",@"Y",@"Z"];
    NSArray * arr = @[@"1",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"];
    self.isOpens = [NSMutableArray arrayWithArray:arr];
    self.provinceCityResult = [[HDPrvinceCityHttpResult alloc]initWithTarget:self action:@selector(provinceLoadData:)];
    self.provinceCityManager = [HDPrvinceCityDataManager shareManager];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([self.provinceCityManager shouldRfreshProvinceData]) {
        //        显示转菊花并发送请求
        [self.tableView.nothingRefreshView show];
        [self requestWithProvince];
    }else
    {
        if (![self.provinceCityManager fmdbIncludeProvince] || ![self.provinceCityManager fmdbIncludeData]){
            
            //        显示转菊花并发送请求
            [self.tableView.nothingRefreshView show];
            [self requestWithProvince];

            
        }else
        {
            self.provinceCityResult.dataArray = [self.provinceCityManager arrayFromFmdb];
        }
    }

    
}
#pragma mark - 网络请求
-(void)requestWithProvince
{
    [self.provinceCityManager loadPrvinceCityData:self.provinceCityResult];
}
#pragma mark - 刷新网络请求
-(void)refreshWihtProvince
{
    [self requestWithProvince];
}
#pragma mark - 数据处理
-(void)provinceLoadData:(HDPrvinceCityHttpResult*)result
{
    [self.tableView endRefreshingAll];
    if (result.success) {
        [self.tableView.nothingRefreshView remove];
        [self.headerViewArray removeAllObjects];
        [self.tableView reloadData];
    }
}


#pragma mark - table view 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  
    return 23;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if ([self.isOpens[section] boolValue]) {
        
        return 1;
    }else
    {
        return 0;
    }
    
       
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    static NSString *CellIdentifier = @"CellIdentifier";
    HDCityCell *cell = [[HDCityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor whiteColor];
            
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    HDProvinceModel * model = self.provinceCityResult.dataArray[0];
    [cell cellWithModel:model.subColumnArray Section:[self.headerTitleArr objectAtIndex:section]];
    
    cell.backgroundView = nil;
    
    return cell;
}
#pragma mark -  headerView
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    HDProvinceHearView * headerView = [[HDProvinceHearView alloc] initWithDelegate:self section:section];
    headerView.title.text = self.headerTitleArr[section];
    if ([self.isOpens[section]integerValue] == 1) {
        headerView.rightImage.transform = CGAffineTransformMakeRotation(M_PI);
    }else
    {
        headerView.rightImage.transform = CGAffineTransformIdentity;
    }
    headerView.delegate =self;
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;

    
    
}
#pragma mark - headerView 高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 56;
}
#pragma mark - 点击headerview 
-(void)headerViewClick:(HDProvinceHearView *)view inSection:(NSInteger)section
{
    NSLog(@"%@",view.rightImage);
    
    for (int i = 0; i < 23; i++) {
        if (i == section) {
            self.isOpens[i] = [NSString stringWithFormat:@"%d",![self.isOpens[i] boolValue]];
           
        } else {
            self.isOpens[i] = @"0";

        }
    }
    

     [self.tableView reloadData];

}

-(void)cellHight:(CGFloat)cellHight
{
    self.cellHight = cellHight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  self.cellHight;
}

#pragma mark - 点击城市
-(void)cellSelectClickHeaderButtonWithButton:(HDCityButton *)sender
{
//    跳转
    HDCityAreaController * cityArea = [[HDCityAreaController alloc]init];
    cityArea.pid = [NSString stringWithFormat:@"%d",sender.tag];
    cityArea.cityName = sender.titleLabel.text;
    cityArea.provinceName = sender.provinceName;
    [self.navigationController pushViewController:cityArea animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end








